package task03;

import java.io.IOException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ApT
 * Date: 20.10.13
 * Time: 22:48
 * To change this template use File | Settings | File Templates.
 */
public class Exceptioner {

    public Exceptioner() {
    }

    /**
     * NullPointerException method generator
     *
     * @throws NullPointerException
     */
    public void nullPointerGenerator() {
        List<String> strings = null;
        strings.size();
    }

    /**
     * IllegalArgument method generator
     *
     * @throws IllegalArgumentException
     */
    public void illegalArgumentGenerator(String text) {
        new Thread().setPriority(Thread.MAX_PRIORITY + 1);
    }

    /**
     * ArrayIndexOfBound method generator
     *
     * @throws ArrayIndexOutOfBoundsException
     */
    public void indexOfBoundGenerator() {
        String[] data = new String[]{"Text", "Two"};
        String text = data[data.length + 10];
    }

    /**
     * ClassCast method generator
     *
     * @throws ClassCastException
     */
    public void classCastGenerator() {
        Object obj1 = new ArrayList<String>();
        Map<String, String> map = (Map<String, String>) obj1;
    }

    /**
     * ClassNotFound method generator
     *
     * @throws ClassNotFoundException
     */
    public void classNotFoundGenerator() throws ClassNotFoundException {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    }

    /**
     * IllegalState method generator
     *
     * @throws IllegalStateException
     */
    public void illegalStateGenerator() {
        List<String> strings = new ArrayList<String>(Arrays.asList(new String[]{"Data", "Some"}));
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()) {
            iterator.remove();
        }
    }

    /**
     * NumberFormat method generator
     *
     * @throws NumberFormatException
     */
    public void numberFormatGenerator() {
        String invalidDigit = "gfsgdfgsa";
        double value = Double.parseDouble(invalidDigit);
    }

    /**
     * CustomException method generator
     *
     * @throws TextCommentException
     */
    public void customExceptionGenerator() throws TextCommentException {
        String text = "\\\\Database";
        if (text.contains("\\\\")) {
            throw new TextCommentException();
        }
    }

    /**
     * OutOfMemory method generator
     *
     * @throws OutOfMemoryError
     */
    public void outOfMemoryGenerator() {
        long[] value = new long[Integer.MAX_VALUE];
    }

    /**
     * StackOverflow method generator
     */
    public void stackOverflowGenerator() {
        Thread t = new Thread() {
            @Override
            public void run() {
                run();
            }
        };
        t.run();
    }

    /**
     * C style exception generator
     * If method returned -1 that is same as NullPointerException in Java
     *
     * @return -1
     */
    private int cStyleNullPointer() {
        String[] array = null;
        if (array == null) {
            return -1;
        }
        return 0;
    }

    private int javaStyleNullPointer() {
        String[] array = null;
        if (array == null) {
            throw new NullPointerException();
        }
        return 0;
    }

    /**
     * Method which measure cost time to perform C style exception
     *
     * @return Time in nanoseconds
     */
    public long cStyleTime() {
        long runTime = System.nanoTime();
        if (cStyleNullPointer() == -1) {
            return System.nanoTime() - runTime;
        }
        return System.nanoTime() - runTime;
    }

    /**
     * Method which measure cost time to perform Java style exception
     *
     * @return Time in nanoseconds
     */
    public long javaStyleTime() {
        long runTime = System.nanoTime();
        try {
            javaStyleNullPointer();
        } catch (NullPointerException e) {
            return System.nanoTime() - runTime;
        }
        return System.nanoTime() - runTime;
    }

    /**
     * Method which burnout JVM
     */
    public void jvmBurnout() {
        Set<String> s = new HashSet<String>();
        ArrayList<String> l;
        Random r = new Random();
        for (int i = 0; i < 20000; i++) {
            s.add(Double.toString(i * r.nextDouble()));
            l = new ArrayList<String>(s);
        }
    }

    /**
     * InternalError method generator
     */
    public void internalErrorGenerator() throws IOException {
        throw new InternalError();
    }
}
